﻿// 
//  MVVM-WPF-WinRt Markup Binding Extensions
//  Copyright © 2013-2014 Alexander Paskhin /paskhin@hotmail.co.uk/ All rights reserved.
// 
//
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using MvvmBindingPack;

namespace WpfDemoApplication.ViewModel
{
    public class MainWindowVMAdditional : MvvmViewModelBase
    {

        /// <summary>
        /// Marker locate property
        /// </summary>
        public int MarkerAdditional { get; set; }


        /// <summary>
        /// Constructor
        /// </summary>
        public MainWindowVMAdditional()
        {

            _commandProxyEmpty = new CommandHandlerProxy(a => { }, a => false);
            _commandProxyInstance = new CommandHandlerProxy(AddExecuteMethod, AddCanExecuteMethod);
            _commandProxy = _commandProxyInstance;
            AddButtonExecuteProperty = AddExecuteMethod;
            AddCanExecuteProperty = AddCanExecuteMethod;
            AddButtonClickPropDelegate = AddButtonClickProp;
        }

      
        /// <summary>
        /// Demo/test methods/ event handlers invocation counter.
        /// </summary>
        int _callCount;
        public int CallCounter
        {
            get { return _callCount++; }
        }

        // Legacy proper handling of Button Command property enable/ disable features.
        readonly CommandHandlerProxy _commandProxyInstance;
        readonly CommandHandlerProxy _commandProxyEmpty;
        CommandHandlerProxy _commandProxy;
        public CommandHandlerProxy CommandProxy
        {
            get
            { return _commandProxy; }
            set
            { _commandProxy = value; NotifyPropertyChanged(); }
        }
         
        /// <summary>
        /// The "can execute" Boolean flag.
        /// </summary>
        bool _canExecuteFlag = true;
        public bool AddCanExecuteFlag
        {
            get { return _canExecuteFlag; }
            set
            {
                _canExecuteFlag = value;
                NotifyPropertyChanged();

                // Legacy proper handling of Button Command property enable/ disable features.
                CommandProxy = value ? _commandProxyInstance : _commandProxyEmpty;

                if (AddActionNotifyCanExecuteChanged != null) { AddActionNotifyCanExecuteChanged(); }
                if (AddEventHandlerNotifyCanExecuteChanged != null) { AddEventHandlerNotifyCanExecuteChanged(this, EventArgs.Empty); }
                if (_propertyDelegateNotifyCanExecuteChanged != null) { _propertyDelegateNotifyCanExecuteChanged(); }
            }
        }

        /// <summary>
        /// Label message for Button Click Method
        /// </summary>
        String _buttonClickMethodMsg;
        public String AddButtonClickMethodMsg
        {
            get { return _buttonClickMethodMsg; }
            set { _buttonClickMethodMsg = value; NotifyPropertyChanged(); }
        }

        /// <summary>
        /// Label message for Button Click PropDel 
        /// </summary>
        String _buttonClickPropMsg;
        public String AddButtonClickPropMsg
        {
            get { return _buttonClickPropMsg; }
            set { _buttonClickPropMsg = value; NotifyPropertyChanged(); }
        }

        /// <summary>
        /// Event Handler for Button Click
        /// </summary>
        public void AddButtonClickMethod(object sender, RoutedEventArgs e)
        {
            AddButtonClickMethodMsg = "Add ButtonClickMethod Called - Counter # " + CallCounter;
        }

        /// <summary>
        /// Event Handler via delegate type property for Button Click
        /// </summary>
        private RoutedEventHandler _buttonClickPropDelegate;
        public RoutedEventHandler AddButtonClickPropDelegate
        {
            get { return _buttonClickPropDelegate; }
            set { _buttonClickPropDelegate = value; NotifyPropertyChanged(); }
        }
        private void AddButtonClickProp(object sender, RoutedEventArgs e)
        {
            AddButtonClickPropMsg = "Add ButtonClickProp Called     - Counter # " + CallCounter;
        }

        /// <summary>
        /// Label message for command Button's group: "Button Cmd-Ex ", "Button Cmd-ExCe", "Button Cmd-ExCeCh"
        /// </summary>
        String _buttonCommandMsgVariantOne;
        public String AddButtonCommandMsgVariantOne
        {
            get { return _buttonCommandMsgVariantOne; }
            set { _buttonCommandMsgVariantOne = value; NotifyPropertyChanged(); }
        }


        public Action<object> AddButtonExecuteProperty { get; set; }
        public void AddExecuteMethod(object sender)
        {
            AddButtonCommandMsgVariantOne = "Add ExecuteMethod Called - Counter # " + CallCounter;
        }

        public Func<object, bool> AddCanExecuteProperty { get; set; }
        public bool AddCanExecuteMethod(object sender)
        {
            return AddCanExecuteFlag;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly")]
        public event Action AddActionNotifyCanExecuteChanged;
        public event EventHandler AddEventHandlerNotifyCanExecuteChanged;


        private Action _propertyDelegateNotifyCanExecuteChanged;
        public Action AddPropertyDelegateNotifyCanExecuteChanged
        {
            get { return _propertyDelegateNotifyCanExecuteChanged; }
            set { _propertyDelegateNotifyCanExecuteChanged = value; }
        }

    }
}
